import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
const { resolve } = require("path");
import AutoImport from "unplugin-auto-import/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

import requireTransform from "vite-plugin-require-transform";
import { VueHooksPlusResolver } from "@vue-hooks-plus/resolvers";

import {
  port,
  open,
  outDir,
  assetsDir,
  chunkSizeWarningLimit,
  cssCodeSplit,
} from "./src/config/index";

const base = import.meta.VITE_APP_BASE_API;

export default defineConfig({
  lintOnSave: true,
  plugins: [
    vue(),
    AutoImport({
      imports: [
        "vue",
        "vue-router",
        "vue-i18n",
        "@vueuse/core",
        "@vueuse/head",
      ],
      include: [/\.[tj]sx?$/, /\.vue$/, /\.vue\?vue/, /\.md$/],
      resolvers: [
        // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
        ElementPlusResolver(),
        // vue-hooks-plus
        VueHooksPlusResolver(),
      ],
      vueTemplate: true, // 是否在 vue 模板中自动导入
      dts: "src/plugins/auto-import.d.ts",
    }),
    requireTransform({
      fileRegex: /.js$|.vue$/,
    }),
  ],
  resolve: {
    alias: {
      "~/": `${resolve(__dirname, ".")}/`,
      "@": `/${resolve(__dirname, "src")}/`,
      "/@/": `/${resolve(__dirname, "src")}/`,
    },
    extensions: [".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
  },
  // 运行ip地址
  server: {
    open: true,
    cors: true,
    hmr: true, //热更新
    proxy: {
      "/zlhx": {
        target: base,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/zlhx/, "zlhx"),
      },
    },
  },
  /* 打包配置 */
  base: "/",
  build: {
    brotliSize: false,
    emptyOutDir: false,
    outDir,
    assetsDir,
    chunkSizeWarningLimit,
    cssCodeSplit,
    rollupOptions: {
      onwarn: () => {
        return;
      },
      // 配置打包输出文件格式，hash每次打包hash都会更新 以方便后续做系统更新提示
      output: {
        chunkFileNames: "static/js/[name]-[hash].js",
        entryFileNames: "static/js/[name]-[hash].js",
        assetFileNames: "static/[ext]/[name]-[hash].[ext]",
      },
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@import '/src/assets/css/flex.scss';`, // 引入全局变量文件
      },
    },
  },
});
